home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / FILEDUMP.C < prev    next >
Text File  |  1985-11-01  |  3KB  |  140 lines

  1. /*
  2.  * Program to read a file and display a hex value and - if possible -
  3.  * ASCII char for each byte read.
  4.  */
  5. #include "stdio.h"
  6. main(argc,argv)
  7.     int argc;
  8.     char *argv[];
  9.     {
  10.     char buffer[0x18],
  11.      exfname[64];
  12.     int  c,
  13.      numoff,
  14.      linecntr,
  15.      buffend,
  16.      buffposn,
  17.      eofinp,
  18.      outfile,
  19.      infile;
  20.     if ((argc == 1) || (argv[1][0] == '?'))
  21.     {
  22.     puts("\n");
  23.     puts("Syntax: FILEDUMP <infile> [<outfile>] [/n]\n");
  24.     puts("where   infile  is the filespec for the input data\n");
  25.     puts("        outfile is the filespec for the output data\n");
  26.     puts("        /n indicates that the output is to be numbered\n");
  27.     puts("\n");
  28.     puts("The input file's characters are output in lines of 24 bytes.");
  29.     puts("\n");
  30.     puts("The hex value of the 24 bytes are listed first in blocks\n");
  31.     puts("of 4, followed by the ASCII representation of the bytes if\n");
  32.     puts("there is one. If the byte is not a printable character, it\n");
  33.     puts("is replaced in the output by a period (\".\").\n");
  34.     puts("If no output file is specified, the output is directed to\n");
  35.     puts("the screen.\n");
  36.     puts("\n");
  37.     exit(0);
  38.     }
  39.     if (argv[1][0] == '#')
  40.     {
  41.     puts("\n");
  42.     puts("PROGRAM: FILEDUMP\n");
  43.     puts("Author : Peter Townsend\n");
  44.     puts("Date   : 31Oct85\n");
  45.     puts("Version: 1.3\n");
  46.     exit(0);
  47.     }
  48.     if ((infile = open(argv[1],0)) == -1)
  49.     {
  50.     printf("\nCannot open input file %s\n",argv[1]);
  51.     exit(1);
  52.     }
  53.     numoff = TRUE;
  54.     if (argc == 2)
  55.     {
  56.     strcpy(exfname,"CON");
  57.     }
  58.     else
  59.     {
  60.     c = ((strcmp(argv[2],"/n") == 0) || (strcmp(argv[2],"/N") == 0));
  61.     if (c)
  62.         {
  63.         strcpy(exfname,"CON");
  64.         numoff = FALSE;
  65.         }
  66.     else
  67.         {
  68.         strcpy(exfname,argv[2]);
  69.         }
  70.     }
  71.     if (strcmp(argv[1],exfname) == 0)
  72.     {
  73.     printf("\nInput filename and output filename cannot be the same.\n");
  74.     exit(1);
  75.     }
  76.     if ((outfile = open(exfname,1)) == -1)
  77.     {
  78.     if ((outfile = creat(exfname)) == -1)
  79.         {
  80.         printf("\nCannot create output file %s\n",exfname);
  81.         exit(1);
  82.         }
  83.     }
  84.     if ((strcmp(argv[3],"/n") == 0) || (strcmp(argv[3],"/N") == 0))
  85.     {
  86.     numoff = FALSE;
  87.     }
  88.     linecntr = 0;
  89.     eofinp = FALSE;
  90.     while (!eofinp)
  91.     {
  92.     buffend = read(infile,buffer,0x18);
  93.     eofinp = (buffend < 0x18);
  94.     buffposn = 0;
  95.     if (!numoff)
  96.         {
  97.         fprintf(outfile,"%04x-",linecntr);
  98.         linecntr = linecntr + buffend - 1;
  99.         fprintf(outfile,"%04x",linecntr);
  100.         }
  101.     while (buffposn < buffend)
  102.         {
  103.         if ((buffposn % 4) == 0)
  104.         {
  105.         fprintf(outfile,"%c",' ');
  106.         }
  107.         fprintf(outfile,"%02x",buffer[buffposn]);
  108.         buffposn++;
  109.         }
  110.     while (buffposn < 24)
  111.         {
  112.         if ((buffposn % 4) == 0)
  113.         {
  114.         fprintf(outfile,"%c",' ');
  115.         }
  116.         fprintf(outfile,"%c%c",' ',' ');
  117.         buffposn++;
  118.         }
  119.     fprintf(outfile,"%c",' ');
  120.     buffposn = 0;
  121.     while (buffposn < buffend)
  122.         {
  123.         if (isprint(buffer[buffposn]))
  124.         {
  125.         fprintf(outfile,"%c",buffer[buffposn]);
  126.         }
  127.         else
  128.         {
  129.         fprintf(outfile,"%c",'.');
  130.         }
  131.         buffposn++;
  132.         }
  133.     fprintf(outfile,"%c",'\n');
  134.     linecntr++;
  135.     }
  136.     fclose(infile);
  137.     fclose(outfile);
  138.     exit(0);
  139.     }
  140.